/**
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 */
package com.jeesite.modules.goods.web;

import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.jeesite.common.config.Global;
import com.jeesite.common.entity.Page;
import com.jeesite.common.web.BaseController;
import com.jeesite.modules.config.MyProps;
import com.jeesite.modules.goods.entity.ZtCouponCode;
import com.jeesite.modules.goods.service.BrandService;
import com.jeesite.modules.goods.service.ZtCouponCodeService;
import com.jeesite.modules.goods.service.ZtGoodsService;

/**
 * 测试数据Controller
 * 
 * @author ThinkGem
 * @version 2018-04-22
 */
@Controller
@RequestMapping(value = "${adminPath}/ztCouponCode")
public class ZtCouponCodeController extends BaseController {
	private String resultImg, imgName;
	
	@Autowired
	private ZtCouponCodeService ztCouponCodeService;
	@Autowired
	private ZtGoodsService ztGoodsService;
	@Autowired
	private BrandService brandService;
	@Autowired
	private MyProps myProps;

	/**
	 * 获取数据
	 */
	@ModelAttribute
	public ZtCouponCode get(String id, boolean isNewRecord) {

		ZtCouponCode ztCouponCode = new ZtCouponCode();
		ztCouponCode.setId(id);
		ztCouponCode = ztCouponCodeService.get(ztCouponCode);
		if (ztCouponCode == null) {
			ztCouponCode = new ZtCouponCode();
		}
		return ztCouponCode;
	}

	/**
	 * 查询列表
	 */
	@RequiresPermissions("ztCouponCode:ztCouponCodeData:view")
	@RequestMapping(value = { "list", "" })
	public String list(ZtCouponCode ztCouponCode, Model model) {
	
		model.addAttribute("ztCouponCode", ztCouponCode);

		return "modules/ztCouponCode/ztCouponCodeList";
	}

	/**
	 * 查询列表数据
	 */
	@RequiresPermissions("ztCouponCode:ztCouponCodeData:view")
	@RequestMapping(value = "listData")
	@ResponseBody
	public Page<ZtCouponCode> listData(ZtCouponCode ztCouponCode,
			HttpServletRequest request, HttpServletResponse response,
			Model model) {
		if(ztCouponCode==null){
			ztCouponCode=new ZtCouponCode();
		}
		
		Page<ZtCouponCode> page = ztCouponCodeService.findPage(new Page<ZtCouponCode>(
				request, response), ztCouponCode);

		return page;
	}

	/**
	 * 查看编辑表单
	 */
	@RequiresPermissions("ztCouponCode:ztCouponCodeData:view")
	@RequestMapping(value = "form")
	public String form(ZtCouponCode ztCouponCode, Model model,
			HttpServletRequest request, HttpServletResponse response) {
		model.addAttribute("ztCouponCode", ztCouponCode);
		
		return "modules/ztCouponCode/ztCouponCodeForm";
	}
	
	/**
	 * 保存数据
	 */
	@RequiresPermissions("ztCouponCode:ztCouponCodeData:edit")
	@PostMapping(value = "save")
	@ResponseBody
	public String save(@Validated ZtCouponCode ztCouponCode,HttpServletRequest request) {
		if(StringUtils.isBlank(ztCouponCode.getId())){
			ztCouponCode.setCreateDate(new Date());
		}
		
		ztCouponCodeService.save(ztCouponCode);
		
		return renderResult(Global.TRUE, text("保存成功！"));
	}

	/**
	 * 删除数据
	 */
	@RequiresPermissions("ztCouponCode:ztCouponCodeData:edit")
	@RequestMapping(value = "delete")
	@ResponseBody
	public String delete(ZtCouponCode ztCouponCode) {
		ztCouponCodeService.delete(ztCouponCode);
		return renderResult(Global.TRUE, text("删除成功！"));
	}
}